﻿// java script chart v1.0.0, saturday Aug 09 17:00:00 CET 2008

// Copyright (c) 2008 
// Author: Xu ChangGong MSN:xuchanggong@hotmail.com GMail:xuchanggong@gmail.com
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Pie.prototype = new jsChart();
Pie.prototype.constructor = jsChart;
Pie.superclass = jsChart.prototype;

function Pie(theXCenter, theYCenter, theOffset, theRadius)
{ 
  this.XCenter=theXCenter;
  this.Offset=theOffset;
  this.YCenter=theYCenter;
  this.Radius=theRadius;
	this.Canvas={};
	this.PieSectors=[];
	this.tabID=0;
}
var _oChart = Pie.prototype;
_oChart._N_Pie=0;
_oChart.XCenter=0;
_oChart.Offset=0;
_oChart.YCenter=0;
_oChart.Radius=0;
_oChart.pre_offset_sector=0;
_oChart.Cursor="";

_oChart.drawChart = function() {
  _totalNum=0;
  for (lk=0; lk<this.datas.length; lk++) {
  	this.datas[lk] = parseFloat(this.datas[lk]);
  	_totalNum+=this.datas[lk];
  }
  legendinitX=0;
  legendinitY=0;
  if(this.legend.placement==JSCHART_RIGHT) {
  	legendinitX = this.XCenter+this.Radius+this.Offset*2;
  	legendinitY = Math.max(0, this.YCenter-(this.datas.length*this.legend.itemHeight)/2/this.legend.columnsNum);
  } else if (this.legend.placement==JSCHART_LEFT) {
  	legendinitX = Math.max(0, this.XCenter-this.Radius-this.Offset*2-(this.legend.itemWidth+10)*this.legend.columnsNum);
  	legendinitY = Math.max(0, this.YCenter-(this.datas.length*this.legend.itemHeight)/2/this.legend.columnsNum);
  } else if (this.legend.placement==JSCHART_TOP) {
  	legendinitX = Math.max(0, this.XCenter-(this.datas.length*this.legend.itemWidth)/2/this.legend.columnsNum);
  	legendinitY = Math.max(0, this.YCenter-this.Radius-this.Offset*2-(this.legend.itemHeight+10)*this.legend.columnsNum);;
  } else if (this.legend.placement==JSCHART_BOTTOM) {
  	legendinitX = Math.max(0, this.XCenter-(this.datas.length*this.legend.itemWidth)/2/this.legend.columnsNum);
  	legendinitY = this.YCenter+this.Radius+this.Offset*2;;
  }
  _prePercent=0;
  _totalPercent=0;
  this.Canvas=this.getDiv(this.canvasID);
  if (this.boder && this.boder.width) {
  	this.Canvas.style.borderWidth = this.boder.width;
		this.Canvas.style.borderColor = this.boder.color;
		this.Canvas.style.borderStyle = this.boder.style;
	}
  this.adjustCanvas();
  eval('pieObj'+this.tabID+'=this');
	if (this.chartTitle && this.chartTitle!='') {
		this.paint(this.Canvas, this.mkDiv(this.chartTitle, 'center', 0, 0, this.canvasWidth, 40, '', 'pietitle_0'+'_'+this.tabID, false, true, ''), true);
	}
  for (lk=0; lk<this.datas.length; lk++) {
  	_ccnum = lk%this.legend.columnsNum;
  	_cccol = parseInt(lk/this.legend.columnsNum);
  	_clix = (this.legend.placement == JSCHART_RIGHT || this.legend.placement == JSCHART_LEFT ? legendinitX+_ccnum*(this.legend.itemWidth+5) : legendinitX+_cccol*(this.legend.itemWidth+5));
  	_cliY = (this.legend.placement == JSCHART_RIGHT || this.legend.placement == JSCHART_LEFT ? legendinitY+_cccol*(this.legend.itemHeight+5) : legendinitY+_ccnum*(this.legend.itemHeight+5));
  	_percent=this.datas[lk]/_totalNum*100;
  	_totalPercent+=_percent;
  	if (lk==this.datas.length-1)
  		_totalPercent=100;
		this.PieSectors[this.PieSectors.length]=new PieSector(this,(_totalPercent-_percent)*3.6,_totalPercent*3.6,this.paints[lk],(lk==0 ? this.Offset : 0));
		this.paint(this.Canvas, this.mkDiv('<font style="font-size:11px;font-family:Arial,Helvetica,sans-serif;color:black;">'+this.legendLabels[lk]+' '+parseInt(_percent*100)/100+'%<\/font>', 'center', _clix, _cliY, this.legend.itemWidth, this.legend.itemHeight, this.paints[lk], 'pieSector_'+lk+'_'+this.tabID, false, true, '','','cursor:pointer;'), true);
		pieSector = this.getDiv('pieSector_'+lk+'_'+this.tabID);
		pieSector.onmouseover=function() {
			secids=this.id.split('_');
			eval('pieObj'+secids[secids.length-1]+'.PieSectors[pieObj'+secids[secids.length-1]+'.pre_offset_sector].MoveTo("","",0)');
			eval('pieObj'+secids[secids.length-1]+'.PieSectors['+secids[secids.length-2]+'].MoveTo("","",10)');
			eval('pieObj'+secids[secids.length-1]+'.pre_offset_sector='+secids[secids.length-2]);
		}
		_prePercent=_percent;
	}
}
PieSector = function(pieObj, theAngle0, theAngle1, theColor, cOffset) {
  this.pieObject=pieObj;
	this.ID="Pie"+this.pieObject._N_Pie+'_'+this.pieObject.tabID; this.pieObject._N_Pie++;
  this.Angle0=theAngle0;
  this.Angle1=theAngle1;
  if ((theColor!="")&&(theColor.length==6)) this.Color="#"+theColor;
  var aa0, aa1, xx, yy, tt, xxo=0, yyo=0, rr2=this.pieObject.Radius*this.pieObject.Radius, xx0, yy0, xx1, yy1, pid180=Math.PI/180, ss0, ss1;
  var nbsp="&nbsp;";
  aa0=this.Angle0;
  aa1=this.Angle1;
  while (aa0>=360) aa0-=360;  
  while (aa0<0) aa0+=360;  
  while (aa1>=360) aa1-=360;    
  while (aa1<0) aa1+=360;
  if (aa0<aa1)
  { xxo=Math.sin((aa0+aa1)*pid180/2)*cOffset;
    yyo=-Math.cos((aa0+aa1)*pid180/2)*cOffset;
  }
  if (aa0>aa1)
  { xxo=Math.sin((aa0+aa1+360)*pid180/2)*cOffset;
    yyo=-Math.cos((aa0+aa1+360)*pid180/2)*cOffset;
  }
	this.pieObject.paint(this.pieObject.Canvas, this.pieObject.mkDiv('', '', Math.round(this.pieObject.XCenter-this.pieObject.Radius+xxo), Math.round(this.pieObject.YCenter-this.pieObject.Radius+yyo), eval(2*this.pieObject.Radius), eval(2*this.pieObject.Radius), '', this.ID), true);
	c_p = this.pieObject.getDiv(this.ID);
	c_p_c='';
  if (aa0==aa1)
  { if (this.Angle0<this.Angle1)
    { for (yy=-this.pieObject.Radius+0.5; yy<this.pieObject.Radius; yy++)
      { xx=Math.round(Math.sqrt(rr2-(yy)*(yy)));
        tt=Math.round(yy+this.pieObject.Radius-0.5);
				c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
    }
  }
  else
  { xx0=Math.sin(aa0*pid180)*this.pieObject.Radius;
    yy0=-Math.cos(aa0*pid180)*this.pieObject.Radius;
    xx1=Math.sin(aa1*pid180)*this.pieObject.Radius;
    yy1=-Math.cos(aa1*pid180)*this.pieObject.Radius;
    for (yy=-this.pieObject.Radius+0.5; yy<0; yy++)
    { xx=Math.round(Math.sqrt(rr2-(yy)*(yy)));
      tt=Math.round(yy+this.pieObject.Radius-0.5);
      if ((yy0>=0)&&(yy1>=0))
      { if (xx0<xx1)
					c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
      else if ((yy0<0)&&(yy1<0))
      { if ((yy<yy0)&&(yy<yy1))
        { if (((xx0<0)&&(xx1>0))||((xx0<0)&&(xx1<=xx0))||((xx1>0)&&(xx0>=xx1)))
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      	}
      	else if ((yy>=yy0)&&(yy>=yy1))
      	{ ss0=Math.round(yy*xx0/yy0);
          ss1=Math.round(yy*xx1/yy1);
          if (xx0<xx1)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss0), tt, Math.round(ss1-ss0), 1, this.Color);
          else
          { 
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(ss1+xx), 1, this.Color);
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss0), tt, Math.round(xx-ss0), 1, this.Color);
          }
        }
        else if (yy>=yy0)
        { ss0=yy*xx0/yy0;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss0), tt, Math.round(xx-ss0), 1, this.Color);
        }
        else
        { ss1=yy*xx1/yy1;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(ss1+xx), 1, this.Color);
        }         
      }
      else if (yy0<0)
      { if (yy>=yy0)
        { ss0=yy*xx0/yy0;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss0), tt, Math.round(xx-ss0), 1, this.Color);
        }
        else if (xx0<0)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
      else
      { if (yy>=yy1)
        { ss1=yy*xx1/yy1;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(ss1+xx), 1, this.Color);
        }
        else if (xx1>0)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
    }
    for (yy=0.5; yy<this.pieObject.Radius; yy++)
    { xx=Math.round(Math.sqrt(rr2-(yy)*(yy)));
      tt=Math.round(yy+this.pieObject.Radius-0.5);
      if ((yy0<=0)&&(yy1<=0))
      { if (xx0>xx1)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
      else if ((yy0>0)&&(yy1>0))
      { if ((yy>yy0)&&(yy>yy1))
        { if (((xx1<0)&&(xx0>0))||((xx1<0)&&(xx0<=xx1))||((xx0>0)&&(xx1>=xx0)))
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      	}
      	else if ((yy<=yy0)&&(yy<=yy1))
      	{ ss0=Math.round(yy*xx0/yy0);
          ss1=Math.round(yy*xx1/yy1);
          if (xx0>xx1)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss1), tt, Math.round(ss0-ss1), 1, this.Color);
          else
          { 
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(ss0+xx), 1, this.Color);
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss1), tt, Math.round(xx-ss1), 1, this.Color);
          }
        }
        else if (yy<=yy0)
        { ss0=yy*xx0/yy0;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(ss0+xx), 1, this.Color);
        }
        else
        { ss1=yy*xx1/yy1;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss1), tt, Math.round(xx-ss1), 1, this.Color);
        }         
      }
      else if (yy0>0)
      { if (yy<=yy0)
        { ss0=yy*xx0/yy0;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(ss0+xx), 1, this.Color);
        }
        else if (xx0>0)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
      else
      { if (yy<=yy1)
        { ss1=yy*xx1/yy1;
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius+ss1), tt, Math.round(xx-ss1), 1, this.Color);
        }
        else if (xx1<0)
						c_p_c+=this.pieObject.mkDiv('', '', Math.round(this.pieObject.Radius-xx), tt, Math.round(2*xx), 1, this.Color);
      }
    }
  }
  this.pieObject.paint(c_p, c_p_c);
  this.pieObject.PieSectors[this.pieObject.PieSectors.length] = this;
}

PieSector.prototype.MoveTo = function (theXCenter, theYCenter, theOffset)
{ var xxo=0, yyo=0, pid180=Math.PI/180, id=this.ID, selObj; 
  if (!isNaN(parseInt(theXCenter))) this.pieObject.XCenter=theXCenter;
  if (!isNaN(parseInt(theYCenter))) this.pieObject.YCenter=theYCenter;
  if (!isNaN(parseInt(theOffset))) this.pieObject.Offset=theOffset;  
  if (this.Angle0< this.Angle1)
  { xxo=Math.sin((this.Angle0+this.Angle1)*pid180/2)*this.pieObject.Offset;
    yyo=-Math.cos((this.Angle0+this.Angle1)*pid180/2)*this.pieObject.Offset;
  }
  if (this.Angle0>this.Angle1)
  { xxo=Math.sin((this.Angle0+this.Angle1+360)*pid180/2)*this.pieObject.Offset;
    yyo=-Math.cos((this.Angle0+this.Angle1+360)*pid180/2)*this.pieObject.Offset;
  }  
  selObj=this.pieObject.getDiv(id);
  with (selObj.style)
  { left=Math.round(this.pieObject.XCenter-this.pieObject.Radius+xxo)+"px";
    top=Math.round(this.pieObject.YCenter-this.pieObject.Radius+yyo)+"px";
    width=eval(2*this.pieObject.Radius)+"px";
    height=eval(2*this.pieObject.Radius)+"px";
    visibility="visible";
  } 
}